/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import DBHandling.DBUtil;
import java.io.IOException;
import javax.swing.JOptionPane;

/**
 *
 * @author HuyPV
 */
public class SettingPanel extends javax.swing.JPanel {

    DBUtil du;
    ItemManagementPanel imp;
    AccountManagementPanel amp;
    CategoryManagementPanel cmp;
    AdministratorPanel adp;

    public SettingPanel() {
        initComponents();
        du = new DBUtil();
        setData();

    }

    private void setData() {
        txtDatabaseName.setText(du.getDbName());
        txtPassword.setText(du.getPass());
        txtPort.setText(du.getPort());
        txtUser.setText(du.getUser());
        txtServer.setText(du.getServer());
    }

    private boolean isValidationForm() {
        String server = txtServer.getText();
        String port = txtPort.getText();
        String user = txtUser.getText();
        String pass = new String(txtPassword.getPassword());
        String dbName = txtDatabaseName.getText();

        if (server.isEmpty()) {
            txtServer.requestFocus();
            txtServer.selectAll();
            JOptionPane.showMessageDialog(this, "The server is not empty!", "Notice", JOptionPane.OK_OPTION);
            return false;
        } else if (port.isEmpty()) {
            txtPort.requestFocus();
            txtPort.selectAll();
            JOptionPane.showMessageDialog(this, "The port is not empty!", "Notice", JOptionPane.OK_OPTION);
            return false;
        } else if (user.isEmpty()) {
            txtUser.requestFocus();
            txtUser.selectAll();
            JOptionPane.showMessageDialog(this, "The user is not empty!", "Notice", JOptionPane.OK_OPTION);
            return false;
        } else if (pass.isEmpty()) {
            txtPassword.requestFocus();
            txtPassword.selectAll();
            JOptionPane.showMessageDialog(this, "The password is not empty!", "Notice", JOptionPane.OK_OPTION);
            return false;
        } else if (dbName.isEmpty()) {
            txtDatabaseName.requestFocus();
            txtDatabaseName.selectAll();
            JOptionPane.showMessageDialog(this, "The database name is not empty!", "Notice", JOptionPane.OK_OPTION);
            return false;
        }
        return true;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar7 = new javax.swing.JToolBar();
        jLabel13 = new javax.swing.JLabel();
        jButton7 = new javax.swing.JButton();
        jLabel14 = new javax.swing.JLabel();
        jButton8 = new javax.swing.JButton();
        jLabel15 = new javax.swing.JLabel();
        jButton9 = new javax.swing.JButton();
        jLabel16 = new javax.swing.JLabel();
        jButton10 = new javax.swing.JButton();
        jLabel17 = new javax.swing.JLabel();
        jButton11 = new javax.swing.JButton();
        jLabel18 = new javax.swing.JLabel();
        jButton12 = new javax.swing.JButton();
        jLabel19 = new javax.swing.JLabel();
        lblBanner = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtServer = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtPort = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtUser = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtPassword = new javax.swing.JPasswordField();
        jLabel6 = new javax.swing.JLabel();
        txtDatabaseName = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));
        setMinimumSize(new java.awt.Dimension(700, 300));
        setPreferredSize(new java.awt.Dimension(747, 591));
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jToolBar7.setBackground(new java.awt.Color(255, 255, 255));
        jToolBar7.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jToolBar7.setFloatable(false);
        jToolBar7.setRollover(true);
        jToolBar7.setEnabled(false);
        jToolBar7.setName("jToolBar7"); // NOI18N

        jLabel13.setText("         "); // NOI18N
        jLabel13.setToolTipText("");
        jLabel13.setName("jLabel13"); // NOI18N
        jToolBar7.add(jLabel13);

        jButton7.setBackground(new java.awt.Color(255, 255, 255));
        jButton7.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jButton7.setForeground(new java.awt.Color(0, 0, 204));
        jButton7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Home-icon48.png"))); // NOI18N
        jButton7.setText("      HOME    ");
        jButton7.setFocusable(false);
        jButton7.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton7.setName("jButton7"); // NOI18N
        jButton7.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });
        jToolBar7.add(jButton7);

        jLabel14.setText("          ");
        jLabel14.setName("jLabel14"); // NOI18N
        jToolBar7.add(jLabel14);

        jButton8.setBackground(new java.awt.Color(255, 255, 255));
        jButton8.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jButton8.setForeground(new java.awt.Color(0, 0, 204));
        jButton8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/accountmanagement 48.png"))); // NOI18N
        jButton8.setText("Account management");
        jButton8.setFocusable(false);
        jButton8.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton8.setName("jButton8"); // NOI18N
        jButton8.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });
        jToolBar7.add(jButton8);

        jLabel15.setText("        ");
        jLabel15.setName("jLabel15"); // NOI18N
        jToolBar7.add(jLabel15);

        jButton9.setBackground(new java.awt.Color(255, 255, 255));
        jButton9.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jButton9.setForeground(new java.awt.Color(0, 0, 204));
        jButton9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/itemsManager_48.png"))); // NOI18N
        jButton9.setText("Item management");
        jButton9.setFocusable(false);
        jButton9.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton9.setName("jButton9"); // NOI18N
        jButton9.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });
        jToolBar7.add(jButton9);

        jLabel16.setText("            ");
        jLabel16.setName("jLabel16"); // NOI18N
        jToolBar7.add(jLabel16);

        jButton10.setBackground(new java.awt.Color(255, 255, 255));
        jButton10.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jButton10.setForeground(new java.awt.Color(0, 0, 204));
        jButton10.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/categoryManage_48.png"))); // NOI18N
        jButton10.setText("Category management");
        jButton10.setFocusable(false);
        jButton10.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton10.setName("jButton10"); // NOI18N
        jButton10.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton10ActionPerformed(evt);
            }
        });
        jToolBar7.add(jButton10);

        jLabel17.setText("           ");
        jLabel17.setName("jLabel17"); // NOI18N
        jToolBar7.add(jLabel17);

        jButton11.setBackground(new java.awt.Color(255, 255, 255));
        jButton11.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jButton11.setForeground(new java.awt.Color(0, 0, 204));
        jButton11.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Settings 48.png"))); // NOI18N
        jButton11.setText("Settings");
        jButton11.setFocusable(false);
        jButton11.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton11.setName("jButton11"); // NOI18N
        jButton11.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton11ActionPerformed(evt);
            }
        });
        jToolBar7.add(jButton11);

        jLabel18.setText("                ");
        jLabel18.setName("jLabel18"); // NOI18N
        jToolBar7.add(jLabel18);

        jButton12.setBackground(new java.awt.Color(255, 255, 255));
        jButton12.setFont(new java.awt.Font("Tahoma", 1, 12));
        jButton12.setForeground(new java.awt.Color(0, 0, 204));
        jButton12.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/help-icon 48.png"))); // NOI18N
        jButton12.setText("   Help   ");
        jButton12.setFocusable(false);
        jButton12.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton12.setName("jButton12"); // NOI18N
        jButton12.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton12.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton12ActionPerformed(evt);
            }
        });
        jToolBar7.add(jButton12);

        jLabel19.setText("                "); // NOI18N
        jLabel19.setName("jLabel19"); // NOI18N
        jToolBar7.add(jLabel19);

        add(jToolBar7, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 590, 840, 90));

        lblBanner.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/banner2.jpg"))); // NOI18N
        lblBanner.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        lblBanner.setName("lblBanner"); // NOI18N
        add(lblBanner, new org.netbeans.lib.awtextra.AbsoluteConstraints(-10, 0, 1200, 100));

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Configuration", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Lucida Handwriting", 0, 14), new java.awt.Color(0, 0, 204))); // NOI18N
        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel2.setText("Server");
        jLabel2.setName("jLabel2"); // NOI18N
        jPanel2.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 40, -1, -1));

        txtServer.setName("txtServer"); // NOI18N
        jPanel2.add(txtServer, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 40, 258, -1));

        jLabel3.setText("Port");
        jLabel3.setName("jLabel3"); // NOI18N
        jPanel2.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 40, -1, -1));

        txtPort.setName("txtPort"); // NOI18N
        jPanel2.add(txtPort, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 40, 130, -1));

        jLabel4.setText("User");
        jLabel4.setName("jLabel4"); // NOI18N
        jPanel2.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 100, 30, -1));

        txtUser.setName("txtUser"); // NOI18N
        jPanel2.add(txtUser, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 100, 180, -1));

        jLabel5.setText("Password");
        jLabel5.setName("jLabel5"); // NOI18N
        jPanel2.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 100, -1, 20));

        txtPassword.setName("txtPassword"); // NOI18N
        jPanel2.add(txtPassword, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 100, 190, -1));

        jLabel6.setText("Database name");
        jLabel6.setName("jLabel6"); // NOI18N
        jPanel2.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 160, -1, -1));

        txtDatabaseName.setName("txtDatabaseName"); // NOI18N
        jPanel2.add(txtDatabaseName, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 160, 250, -1));

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/refresh.png"))); // NOI18N
        jButton1.setText("Reset");
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 220, -1, -1));

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/save.png"))); // NOI18N
        jButton2.setText("Save");
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 220, 87, -1));

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/ok.png"))); // NOI18N
        jButton3.setText("Check connection");
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 220, -1, 31));

        add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 160, 510, 310));
    }// </editor-fold>//GEN-END:initComponents

        private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton11ActionPerformed
    }//GEN-LAST:event_jButton11ActionPerformed

    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed
        try {
            // TODO add your handling code here:
            if (cmp == null) {
                cmp = new CategoryManagementPanel();
            }
        } catch (Exception ex) {
        }
        InventoryManagement.callJPanel(cmp);
    }//GEN-LAST:event_jButton10ActionPerformed

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
        if (amp == null) {
            amp = new AccountManagementPanel();
        }
        InventoryManagement.callJPanel(amp);
    }//GEN-LAST:event_jButton8ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        if(adp==null)
            adp = new AdministratorPanel();
        InventoryManagement.callJPanel(adp);
    }//GEN-LAST:event_jButton7ActionPerformed

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
        // TODO add your handling code here:
        if (imp == null) {
            imp = new ItemManagementPanel();
        }
        InventoryManagement.callJPanel(imp);
    }//GEN-LAST:event_jButton9ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        txtDatabaseName.setText("");
        txtPassword.setText("");
        txtPort.setText("");
        txtServer.setText("");
        txtUser.setText("");
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        String server = txtServer.getText();
        String port = txtPort.getText();
        String user = txtUser.getText();
        String pass = new String(txtPassword.getPassword());
        String dbName = txtDatabaseName.getText();
        if (isValidationForm()) {
            du = new DBUtil(server, port, user, pass, dbName);
            if (du.saveConfig("config.ims")) {
                JOptionPane.showMessageDialog(this, "Save information successfully!", "Notice", JOptionPane.INFORMATION_MESSAGE);
            }
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        String server = txtServer.getText();
        String port = txtPort.getText();
        String user = txtUser.getText();
        String pass = new String(txtPassword.getPassword());
        String dbName = txtDatabaseName.getText();
        if (isValidationForm()) {
            du = new DBUtil(server, port, user, pass, dbName);
            if (du.checkConnection()) {
                JOptionPane.showMessageDialog(this, "Connected!", "Notice", JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(this, "Not connected!", "Notice", JOptionPane.INFORMATION_MESSAGE);
            }
        }

    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton12ActionPerformed
        // TODO add your handling code here:

        try {
            // TODO add your handling code here:
            InventoryManagement.OpenManual();
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, "cannot locate manual file in 'manual/ISM_Manual.htm'");
        }
    }//GEN-LAST:event_jButton12ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton11;
    private javax.swing.JButton jButton12;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JToolBar jToolBar7;
    private javax.swing.JLabel lblBanner;
    private javax.swing.JTextField txtDatabaseName;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtPort;
    private javax.swing.JTextField txtServer;
    private javax.swing.JTextField txtUser;
    // End of variables declaration//GEN-END:variables
}
